package com.mallcai.bigdata.ladon.dpl.vo;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
public enum DataType {
    STRING("STRING", "字符串"),
    INTEGER("INTEGER", "整数"),
    LONG("LONG", "长整数"),
    DATE("DATE", "日期"),
    YEAR("YEAR", "年份"),
    MONTH("MONTH", "月份"),
    TIME("TIME", "时间"),
    DATETIME("DATETIME", "日期时间"),
    TIMESTAMP("TIMESTAMP", "时间戳"),
    BIG_DECIMAL("BIG_DECIMAL", "精确数据类型"),
    BLOB("BLOB", "二进制"),
    DOUBLE("DOUBLE", "双精度浮点数"),
    FLOAT("FLOAT", "单精度浮点数"),
    BOOLEAN("BOOLEAN", "布尔类型"),
    OBJECT("OBJECT", "对象");
    private String code;
    private String desc;

    DataType(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    private static final Map<String, DataType> CACHE = new HashMap<>();

    static {
        for (DataType dt : DataType.values()) {
            CACHE.put(dt.code, dt);
        }
    }

    public static DataType getByCode(String code) {
        return parse(code);
    }


    public static DataType getByCodeWithDefault(String code, DataType defaultValue) {
        Objects.requireNonNull(code, "参数不能为空");
        return CACHE.getOrDefault(code.toUpperCase(), defaultValue);
    }

    public static DataType parse(String type){
        Objects.requireNonNull(type, "参数不能为空");
        type = type.toUpperCase();
        if (CACHE.containsKey(type)){
            return CACHE.get(type);
        }
        // 整形
        if (StringUtils.equalsIgnoreCase("TINYINT", type)
                || StringUtils.equalsIgnoreCase("SMALLINT", type)
                || StringUtils.equalsIgnoreCase("MEDIUMINT", type)
                || StringUtils.equalsIgnoreCase("INT", type)) {
            return INTEGER;
        }
        // 长整形
        if (StringUtils.equalsIgnoreCase("BIGINT", type)
                || StringUtils.equalsIgnoreCase("LONG", type)) {
            return LONG;
        }
        //字符串
        if (StringUtils.equalsIgnoreCase("CHAR", type)
                || StringUtils.equalsIgnoreCase("VARCHAR", type)
                || StringUtils.equalsIgnoreCase("TEXT", type)) {
            return STRING;
        }

        if (StringUtils.equalsIgnoreCase("DECIMAL", type)) {
            return BIG_DECIMAL;
        }
        return OBJECT;
    }
}
